package gui

import (
	"wxp2p/clientN"
	theme2 "wxp2p/uiapp/theme"

	"fyne.io/fyne/v2/theme"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
)

type GlobalVars struct {
	CurrentWindow fyne.Window
	APP           fyne.App
	localClientId string
	server        *clientN.ServerClient
	client        *clientN.ClientClient
	signalAddr    string
}

func IniGVars(gVars *GlobalVars) {
	gVars.signalAddr = gVars.APP.Preferences().StringWithFallback("signalAddr", "43.138.80.3:8018")
	gVars.server = clientN.NewServerClient(gVars.signalAddr, "")
}

// Start .
func Start(gVars *GlobalVars) {
	myWindow := gVars.CurrentWindow
	tabs := container.NewAppTabs(
		container.NewTabItemWithIcon("远程控制", theme.ComputerIcon(), container.NewPadded(remoteConWindow(gVars))),
		container.NewTabItemWithIcon("文件传输", theme.FileIcon(), container.NewPadded(fileTransWindow(gVars))),
		container.NewTabItemWithIcon("端口映射", theme.UploadIcon(), container.NewPadded(portMapWindow(gVars))),
		container.NewTabItemWithIcon("高级设置", theme.SettingsIcon(), container.NewPadded(settingsWindow(gVars))),
	)

	tabs.OnSelected = func(t *container.TabItem) {
		t.Content.Refresh()
	}

	myWindow.SetContent(tabs)
	// myWindow.Resize(fyne.NewSize(myWindow.Canvas().Size().Width, myWindow.Canvas().Size().Height))
	myWindow.Resize(fyne.NewSize(440, myWindow.Canvas().Size().Height))
	myWindow.CenterOnScreen()
	myWindow.SetMaster()
	myWindow.ShowAndRun()
}

// InitNewScreen .
func InitNewScreen() *GlobalVars {
	myApp := app.NewWithID("p2p-rdct")

	t := myApp.Preferences().StringWithFallback("Theme", "Light")
	myApp.Settings().SetTheme(&theme2.MyTheme{Theme: t})
	myApp.SetIcon(theme2.Ico)
	myWindow := myApp.NewWindow("远程端口映射")

	return &GlobalVars{
		CurrentWindow: myWindow,
		APP:           myApp,
	}
}
